<?php
include_once("./function.php");

//print_r($_GET);die;//Array ( [id] => 0 [pname] => 服装 [show] => noshow [sub] => 添加 )

extract($_GET);//$id $pname

//2数据处理
$id=trim($id);
$pname=trim($pname);
$show=trim($show);
//判断分类名是否为空
if(empty($pname))
{
	msg("分类名不能为空","product_cate.php");exit;
}

//4分类名不能重复
$sql="select * from ts_product_cate where pc_name='$pname'";
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	if(mysqli_num_rows($res)==1)
	{
		msg("添加的分类名不能重复","product_cate.php");exit;
	}
}
//5得到level
if($id==0)
{
  $level=1;
}
else
{
	$sql="select * from ts_product_cate where pc_id=$id";
	$res=mysqli_query($link,$sql);
	if(is_object($res))
	{
		if(mysqli_num_rows($res)==1)
		{
			$list=mysqli_fetch_assoc($res);
			$level=$list['pc_level']+1;
		}
	}
}

//6入库
$sql="insert into ts_product_cate(pc_fid,pc_name,pc_level,pc_ifshow)values('$id','$pname','$level','$show')";
$res=mysqli_query($link,$sql);
if($res)
{
	if(mysqli_affected_rows($link)==1)
	{
		msg("添加成功","./product_cate.php");exit;
	}
	else
	{
		msg("添加失败","product_cate.php");exit;
	}
}
else
{
	echo "错误码:".mysqli_errno($link)."错误信息:".mysqli_error($link);exit;
}
?>